package Course13;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.File;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class CodeSearch {
    public static void main(String[] args)throws Exception {
        //读取Downloads文件夹gaode.xlsx
        FileInputStream in = new FileInputStream(new File("C:/Users/lenovo/Desktop/gaode.xlsx"));

        Workbook workbook = new XSSFWorkbook(in);

        Sheet sheet = workbook.getSheetAt(0);

        int rowNum = sheet.getLastRowNum();
        //定义一个hanhmap 储存城市，abcode的key值，value是city的对象
        Map<String ,City> map = new HashMap<>();

        for (
                int i = 2;
                i < rowNum; i++)
        {
            City city = new City();
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String abcode = row.getCell(1).getStringCellValue();
            city.setAbcode(abcode);
            city.setName(name);
            map.put(city.getAbcode(),city);
            //System.out.println(map.get(abcode).getName());
            //System.out.println(map.get(abcode).getAbcode());
        }
        //IO
        String string = null;
        //创建一个对象后，我们可以使用reader（）方法从控制台读取一个字符，或者用readline（）读取一个字符串
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in ));
        System.out.println("请输入想查询的邮编");
        System.out.println("输入结束可结束程序");
        //读取字符 IO一直存在
        do{
            try {
                string = bufferedReader.readLine();
                if(string.equals("结束"))
                {}
                else if (!map.containsKey(string)){
                    System.out.println("未找到该邮编对应的城市，请输入正确的邮编");
                }else {
                    City city1 = map.get(string);
                    String youbian = city1.getAbcode();
                    char[] ch = youbian.toCharArray();
                    String yier = String.valueOf(ch[0]) + ch[1];
                    String sansi = String.valueOf(ch[2]) + ch[3];
                    String wuliu = String.valueOf(ch[4]) + ch[5];
                    String zhixiashi = yier + "00" + "00";
                    String zhixiashiqu = yier + "01" + wuliu;
                    City zhixia = map.get(zhixiashi);

                    //直辖市
                    if (yier.equals("11") || yier.equals("12") || yier.equals("31") || yier.equals("50")) {
                        //城市下辖的直辖区的判断
                        if(sansi.equals("01")||sansi.equals("02")){
                            System.out.println("区名称" + city1.getName());
                            System.out.println("区邮编" + city1.getAbcode());
                            System.out.println("它属于的直辖市："+zhixia.getName());
                        }else if(sansi.equals("00")){
                        System.out.println("直辖市名称" + city1.getName());
                        System.out.println("直辖市邮编" + city1.getAbcode());
                        System.out.println("它下属的区：");
                          //遍历直辖市下辖的区
                            for(int i =0;i<99;i++){
                                String str = String.valueOf(i);
                                // i<10用来表示后两位的数
                                if(i<10){
                                    str = "0"+i;
                                }
                                String qu = yier + "01" + str;
                                //重庆的特殊关照 县
                                String qu2 = yier + "02" + str;
                                City city2 = map.get(qu);
                                City xian = map.get(qu2);
                                //城市非空的判断
                                if(city2!=null){
                                    System.out.println("区名称" + city2.getName());
                                    System.out.println("区邮编" + city2.getAbcode());
                                    System.out.println("    ");
                                }
                                //重庆市区的判断
                                if(xian!= null){
                                    System.out.println("区名称" + xian.getName());
                                    System.out.println("区邮编" + xian.getAbcode());
                                    System.out.println("     ");
                                }
                            }
                        }
                    }
                }
                //System.out.println("城市名称" + city1.getName());
                //System.out.println("城市邮编" + city1.getAbcode());}
            }catch (Exception e){
              e.printStackTrace();
            }
        }while(!string.equals("结束"));
    }
}
